#include "SampleBase.h"
#include "Win32Helper.h"
#include "D3DSystem.h"


bool SampleBase::Init(HINSTANCE hInstance, int windowWidth, int windowHeight, bool windowed)
{
	HWND hwnd = Win32Helper::CreateApplicationWindow(hInstance, windowWidth, windowHeight);
	if (hwnd == NULL)
		return false;

	if (!D3DSystem::InitD3D(windowWidth, windowHeight, windowed, hwnd))
		return false;
	
	m_device  = D3DSystem::GetDevice();

	m_windowWidth  = windowWidth;
	m_windowHeight = windowHeight;
	m_windowed     = windowed;

	return true;
}

bool SampleBase::Run()
{
	Setup();	
	EnterMsgLoop();
	Cleanup();

	m_device->Release();
	return true;
}

void SampleBase::EnterMsgLoop()
{
	MSG msg;
	::ZeroMemory(&msg, sizeof(MSG));

	static float lastTime = (float)timeGetTime(); 

	while(msg.message != WM_QUIT)
	{
		if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		{
			::TranslateMessage(&msg);
			::DispatchMessage(&msg);
		}
		else
		{	
			float currTime  = (float)timeGetTime();
			float timeDelta = (currTime - lastTime)*0.001f;

			Display(timeDelta);

			lastTime = currTime;
		}
	}
	
}

